package com.woniuxy.microfixeddepositserver.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bean.bank.BankBean;
import com.woniuxy.microfixeddepositserver.dao.BankDao;
import com.woniuxy.microfixeddepositserver.service.IBankService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author: masterhai
 * @date: 2021/4/21:11:07
 * @description:
 */
@Service
public class BankServiceImpl implements IBankService {
    @Resource
    private BankDao bankDao;

    @Override
    public PageInfo<BankBean> selectALL (Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<BankBean> bankBeans = bankDao.selectALL();
        List<BankBean> bankBeans1 = updateUnit(bankBeans);
        PageInfo<BankBean> pageInfo = new PageInfo<>(bankBeans1);
        return pageInfo;
    }

    @Override
    public PageInfo<BankBean> selectALLByQuery (Integer page, Integer limit, String bankName) {
        PageHelper.startPage(page, limit);
        List<BankBean> bankBeans = bankDao.selectALLByQuery("%" + bankName + "%");
        List<BankBean> bankBeans1 = updateUnit(bankBeans);
        PageInfo<BankBean> pageInfo = new PageInfo<>(bankBeans1);
        return pageInfo;
    }

    @Override
    public int updateBank (BankBean bankBean) {
        return bankDao.updateBank(bankBean);
    }

    @Override
    public int deleteBank (Integer id) {
        return bankDao.deleteBank(id);
    }

    @Override
    public int addBank (BankBean bankBean) {
        return bankDao.addBank(bankBean);
    }

    @Override
    public BankBean getBankById (Integer id) {
        return bankDao.getBankById(id);
    }

    private List<BankBean> updateUnit (List<BankBean> bankList) {
        //更改银行对象限额的单位
        for (int i = 0; i < bankList.size(); i++) {
            BankBean bank = bankList.get(i);
            Long dayLimit = Long.valueOf(bank.getDayLimit());
            Long sinLimit = Long.valueOf(bank.getSingleLimit());
            //修改限额单位
            if (dayLimit >= 10000 && dayLimit % 10000 == 0) {
                bank.setDayLimit(dayLimit / 10000 + "万");
            } else if (dayLimit >= 1000 && dayLimit % 1000 == 0) {
                bank.setDayLimit(dayLimit / 1000 + "千");
            } else {
                bank.setDayLimit(dayLimit+"");
            }
            if (sinLimit >= 10000 && sinLimit % 10000 == 0) {
                bank.setSingleLimit(sinLimit / 10000 + "万");
            } else if (sinLimit >= 1000 && sinLimit % 1000 == 0) {
                bank.setSingleLimit(sinLimit / 1000 + "千");
            } else {
                bank.setSingleLimit(sinLimit + "");
            }
        }
        return bankList;
    }
}
